package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInDefaultSNsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInSNDetailsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInSNsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInPositionBean;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/4 11:12
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInSNDetailsAdapter extends BaseAdapter {

    private Context context;
    private String condition;
    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;
    private int outNum;//出库数量
    private int inNum = 0;//入库数量
    private List<AllocationInPositionBean> list;
    private AllocationInSNDetailsActivity.ClickListener listener;
    private String unit;

    public AllocationInSNDetailsAdapter(Context context, String condition, AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean,
                                        List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> allList, AllocationInSNDetailsActivity.ClickListener listener) {
        this.context = context;
        this.condition = condition;
        this.itemListBean = itemListBean;
        outNum = allList.size() - 1;//因为包含默认仓位
        inNum += AllocationUtils.getInNumFromListByBatchOrSnIsOk(allList) - 1;
        inNum += AllocationUtils.getInNumFromDefaultListByMatId(context, itemListBean.getMat_id());
        list = AllocationUtils.getShowList(allList);
        this.listener = listener;
        this.unit = itemListBean.getUnit_code();
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cy_view_in_storage_allocation_mat_first, null);
            //信息
            TextView codeTv = (TextView) convertView.findViewById(R.id.codeTv);
            TextView describeTv = (TextView) convertView.findViewById(R.id.describeTv);
            TextView outNumTv = (TextView) convertView.findViewById(R.id.outNumTv);
            TextView inNumTv = (TextView) convertView.findViewById(R.id.inNumTv);
            //搜索
            EditText conditionEdit = (EditText) convertView.findViewById(R.id.conditionEdit);
            ImageView searchImg = (ImageView) convertView.findViewById(R.id.searchImg);

            codeTv.setText(itemListBean.getMat_code());
            describeTv.setText(itemListBean.getMat_name());
            outNumTv.setText(String.valueOf(outNum + unit));
            inNumTv.setText(String.valueOf(inNum + unit));
            conditionEdit.setText(condition);

            searchImg.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(context, v);
                    listener.onNoDoubleClickListener(conditionEdit.getText().toString());
                    conditionEdit.setText("");
                }
            });

        } else {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.cy_view_in_storage_allocation_mat_sn_item, null);
                viewHolder = new ViewHolder();
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            AllocationInPositionBean bean = list.get(position - 1);
            initData(context, viewHolder, convertView, bean);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView codeTv;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.codeTv = (TextView) view.findViewById(R.id.codeTv);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, AllocationInPositionBean bean) {

        viewHolder.codeTv.setText(bean.getPosition_code());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (bean.getPosition_code().equals(AllocationUtils.NO_CODE_POSITION)) {
                    //默认仓位
                    Intent intent = new Intent(context, AllocationInDefaultSNsActivity.class);
                    intent.putExtra(AllocationInStorageExtra.MAT_ID, itemListBean.getMat_id());
                    context.startActivity(intent);
                } else {
                    //正常仓位
                    Intent intent = new Intent(context, AllocationInSNsActivity.class);
                    intent.putExtra(AllocationInStorageExtra.MAT_ID, itemListBean.getMat_id());
                    intent.putExtra(AllocationInStorageExtra.POSITION_CODE, bean.getPosition_code());
                    context.startActivity(intent);
                }
            }
        });

    }

    public int getOutNum() {
        return outNum;
    }

    public int getInNum() {
        return inNum;
    }
}
